package gecko.view.swing;

import gecko.model.Connect;
import gecko.model.Requete;

import java.awt.Component;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.EventObject;

import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class VTab extends AbstractTableModel implements TableModelListener{
	private String[] entetes;
	private Object[][] donnees;
	private boolean DEBUG = false;
	PropertyChangeSupport propertyChangeSupport;

	public VTab(String[] entetes, Object[][] donnee) {
		this.entetes = entetes;
		this.donnees = donnee;
		propertyChangeSupport =  new PropertyChangeSupport(this);
	}

	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return entetes.length;
	}
	
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return donnees.length;
	}

	@Override
	public String getColumnName(int col) {
		return entetes[col];
	}


	@Override
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		return donnees[row][col];
	}
	
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row,int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 10) {
            return false;
        } else {
            return true;
        }
    }
    
	public void setValueAt(Object value, int row, int col) {
		donnees[row][col] = value;
		fireTableCellUpdated(row,col);
	}
       
    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + donnees[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
    

	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
		Connect con = new Connect();
		Statement state = null;
		try {
			state = con.dbConnect().createStatement();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        int row = e.getFirstRow();
        int column = e.getColumn();
        this.removeTableModelListener(this);
        Object data = this.getValueAt(row, column);
        String val = data.toString();
        boolean statut = Boolean.valueOf(val);
        
        if (statut == Boolean.TRUE) {
        	//this.setValueAt(true, row, column);

        	try {
				state.executeUpdate("UPDATE contrat SET `etat` = '0' WHERE id_contrat = '"+this.getValueAt(row, 0)+"'");
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        	this.addTableModelListener(this);
        }
        
        else if (statut == Boolean.FALSE){
        	//this.setValueAt(false, row, column);

        	try {
				state.executeUpdate("UPDATE contrat SET `etat` = '1' WHERE id_contrat = '"+this.getValueAt(row, 0)+"'");
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
        	this.addTableModelListener(this);
        	
        }
        System.out.println("ligne : "+row+" et colonne : "+column+ " :" + statut);
        //System.out.println(this.getValueAt(row, column));
     
	}
	
}


